package com.jmxcfc.blfsc.ninefour.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jmxcfc.blfsc.common.sms.enums.MsgTypeEnum;
import com.jmxcfc.blfsc.common.sms.enums.SendStatusEnum;
import com.jmxcfc.blfsc.common.sms.notify.NotifyDataItem;
import com.jmxcfc.blfsc.common.sms.notify.ResultNotifyRequest;
import com.jmxcfc.blfsc.ninefour.common.Constants;
import com.jmxcfc.blfsc.ninefour.entity.SendDetailSub;
import com.jmxcfc.blfsc.ninefour.entity.SmsNotifyRecord;
import com.jmxcfc.blfsc.ninefour.entity.VoiceNotifyRecord;
import com.jmxcfc.blfsc.ninefour.feign.ISmsClientService;
import com.jmxcfc.blfsc.ninefour.util.SpringBeanUtils;
import com.jmxcfc.fs.common.IntegerEnum;
import com.jmxcfc.fs.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *异步发起调用blfs-sms 进行发送状态(语音发送状态或者短信发送状态) 通知
 */
@Slf4j
public class SyncSendNotifyResultOperator implements Runnable {
    private String tag;
    private SendDetailSub sendDetailSub;
    private SmsNotifyRecord smsNotifyRecord;
    private VoiceNotifyRecord voiceNotifyRecord;

    /**
     * 1、挂机短信回调接口, smsNotifyRecord必传值
     * 2、ai挂断回调(语音外呼), voiceNotifyRecord必传值
     * @param tag
     * @param sendDetailSub
     * @param smsNotifyRecord
     * @param voiceNotifyRecord
     */
    public SyncSendNotifyResultOperator(String tag,SendDetailSub sendDetailSub,SmsNotifyRecord smsNotifyRecord,VoiceNotifyRecord voiceNotifyRecord) {
        this.tag = tag;
        this.sendDetailSub = sendDetailSub;
        this.smsNotifyRecord= smsNotifyRecord;
        this.voiceNotifyRecord= voiceNotifyRecord;
    }

    /**
     * todo 缺少处理发生异常时,重推数据
     */
    @Override
    public void run() {
        try {
            ISmsClientService smsClientService = SpringBeanUtils.getBean(ISmsClientService.class);
            ResultNotifyRequest<JSONObject> resultNotifyRequest = buildNotifyParam(sendDetailSub, smsNotifyRecord, voiceNotifyRecord);
            log.info("异步线程处理: 94语音-回调处理,tag【标识一条短信的流水号uuid】:{},发送状态异步通知调用blfs-sms,调用参数:{}", tag,JSON.toJSONString(resultNotifyRequest));
            Result<Object> result = smsClientService.receiveNotify(JSON.parseObject(JSON.toJSONString(resultNotifyRequest)));
            log.info("异步线程处理:94语音-回调处理,tag【标识一条短信的流水号uuid】:{},发送状态异步通知调用blfs-sms,响应结果:{}",tag,result);
        } catch (Exception e) {
            log.error("94语音-回调处理,tag【标识一条短信的流水号uuid】:{},发送状态异步通知处理异常,调用blfs-sms 异常,异常信息:{}", tag, e.getMessage(), e);
            //throw new NotifyException(e);
        }

    }

    /**
     * 参数组织
     * 1、挂机短信回调接口, smsNotifyRecord必传值
     * 2、ai挂断回调(语音外呼), voiceNotifyRecord必传值
     * @param sendDetailSub
     * @param smsNotifyRecord
     * @param voiceNotifyRecord
     * @return
     */
    private ResultNotifyRequest<JSONObject> buildNotifyParam(SendDetailSub sendDetailSub, SmsNotifyRecord smsNotifyRecord,VoiceNotifyRecord voiceNotifyRecord) {
        ResultNotifyRequest<JSONObject> notifyRequest = ResultNotifyRequest.of(MsgTypeEnum.VOICE);
        if (Objects.nonNull(sendDetailSub)) {
            notifyRequest.setRequestNo(sendDetailSub.getRequestNo());
            notifyRequest.setBatchId(sendDetailSub.getRequestId());
        }
        NotifyDataItem notifyDataItem = new NotifyDataItem();
        if (Objects.nonNull(smsNotifyRecord)) {
            // 挂机短信回调
            notifyDataItem.setUuid(smsNotifyRecord.getTag());
            notifyDataItem.setMobile(smsNotifyRecord.getCellPhoneNo());
            notifyDataItem.setMsgTotal(String.valueOf(smsNotifyRecord.getSmsCount()));
            //短信最终发送状态 (1:发送成功、 2:发送失败)
            notifyDataItem.setSmsSendStatus(StringUtils.equals(smsNotifyRecord.getStatus(), Constants.STRING_1) ? SendStatusEnum.SUCCESS : SendStatusEnum.FAILURE);
            notifyDataItem.setSmsSendStatusDesc(notifyDataItem.getSmsSendStatus().desc());
            log.info("94语音-【挂机短信回调】,tag【标识一条短信的流水号uuid】:{},短信最终发送状态(1:发送成功、 2:发送失败)的值:{},转换成JMX发生状态:{}", smsNotifyRecord.getTag(), smsNotifyRecord.getStatus(), notifyDataItem.getSmsSendStatus());
            notifyDataItem.setErrorCode(smsNotifyRecord.getStatus());
            //通道状态码(短信通道返回的原始短信状态码)
            notifyDataItem.setErrorDesc(smsNotifyRecord.getChannelResult());
        }else {
            //ai挂断回调(外呼回调)
            notifyDataItem.setUuid(voiceNotifyRecord.getTag());
            notifyDataItem.setMobile(voiceNotifyRecord.getCellPhoneNo());
            //notifyDataItem.setMsgTotal();

            //短信最终发送状态 (1 "已接听",2"关机",3 "空号",4 "改号",
            // 5 "停机",6 "占线",7 "拒接",8 "无法接通",9 "未接",10"用户欠费",
            // 11 "线路故障",12 "呼叫失败" 30 "已拦截" 40 "单日呼叫次数受限")
            notifyDataItem.setVoiceSendStatus(StringUtils.equals(voiceNotifyRecord.getStatusCode(), Constants.STRING_1) ? SendStatusEnum.SUCCESS : SendStatusEnum.FAILURE);
            notifyDataItem.setVoiceSendStatusDesc(notifyDataItem.getVoiceSendStatus().desc());
            log.info("94语音-【ai挂断回调(外呼回调)】,tag【标识一条短信的流水号uuid】:{},语音外呼状态的值:{},转换成JMX发生状态:{}", voiceNotifyRecord.getTag(), voiceNotifyRecord.getStatusCode(), notifyDataItem.getVoiceSendStatus());
            //外呼状态编码
            notifyDataItem.setErrorCode(voiceNotifyRecord.getStatusCode());
            //外呼状态描述
            notifyDataItem.setErrorDesc(voiceNotifyRecord.getStatusDescription());
        }

        List<NotifyDataItem> notifyDataItemList = new ArrayList<>(IntegerEnum.INTEGER_1.value());
        notifyDataItemList.add(notifyDataItem);
        notifyRequest.setNotifyDataList(notifyDataItemList);
        notifyRequest.setNotifyDataList(notifyDataItemList);
        notifyRequest.setEndTime(LocalDateTime.now());
        return notifyRequest;
    }



}
